package com.zondy.mapgisdrawtool;

import java.io.FileNotFoundException;

import android.app.Activity;
import android.os.Bundle;
import android.widget.Button;

import com.gis_luq.lib.Draw.DrawEvent;
import com.gis_luq.lib.Draw.DrawEventListener;
import com.gis_luq.lib.Draw.DrawTool;
import com.zondy.mapgis.android.environment.Environment;
import com.zondy.mapgis.android.graphic.Graphic;
import com.zondy.mapgis.android.graphic.GraphicPolylin;
import com.zondy.mapgis.android.graphic.GraphicsOverlay;
import com.zondy.mapgis.android.mapview.MapView;
import com.zondy.mapgis.core.map.Document;

public class MainActivity extends Activity implements DrawEventListener
{
	private MapView mMapView = null;
	private Graphic selectGraphic = null;
	private DrawTool drawTool;
	private Document mDoc = new Document();

	private GraphicsOverlay mMainGraphicsLayer = null;
	private GraphicPolylin mGraphicPolylin ;
	private Graphic mGraphic = null;

	@Override
	protected void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);

		Environment.initialize(FilePathCfg.PHONE_SDCARD_PATH + "/MapGIS Sample/", this);
		com.zondy.mapgis.android.environment.Environment.requestAuthorization(this, new Environment.AuthorizeCallback()
		{
			@Override
			public void onComplete()
			{
				initView();
			}
		});
		
	}

	/**
	 * 初始化窗体
	 */
	private void initView()
	{
		mMapView = (MapView) findViewById(R.id.mapview);

//		mMapView.loadFromFile(FilePathCfg.MAPX_FILE_PATH);
		mDoc.open(FilePathCfg.MAPX_FILE_PATH);
		mMapView.loadFromDocument(mDoc, 0);
		

		mMainGraphicsLayer = new GraphicsOverlay();
		mMapView.getGraphicsOverlays().add(mMainGraphicsLayer);

		// 初始化DrawTool实例
		this.drawTool = new DrawTool(this.mMapView);
		// 将本Activity设置为DrawTool实例的Listener
		this.drawTool.addEventListener(this);

		ToolsOnClickListener toolsOnClickListener = new ToolsOnClickListener(this, drawTool, selectGraphic, mMapView);
		Button btnDrawPoint = (Button) this.findViewById(R.id.btnDrawPoint);
		btnDrawPoint.setOnClickListener(toolsOnClickListener);

		Button btnDrawPolyline = (Button) this.findViewById(R.id.btnDrawPolyline);
		btnDrawPolyline.setOnClickListener(toolsOnClickListener);

		Button btnDrawFreePolyline = (Button) this.findViewById(R.id.btnDrawFreePolyline);
		btnDrawFreePolyline.setOnClickListener(toolsOnClickListener);

		Button btnDrawPolygon = (Button) this.findViewById(R.id.btnDrawPolygon);
		btnDrawPolygon.setOnClickListener(toolsOnClickListener);

		Button btnDrawFreePolygon = (Button) this.findViewById(R.id.btnDrawFreePolygon);
		btnDrawFreePolygon.setOnClickListener(toolsOnClickListener);

		Button btnDrawCircle = (Button) this.findViewById(R.id.btnDrawCircle);
		btnDrawCircle.setOnClickListener(toolsOnClickListener);

		Button btnDrawEnvlope = (Button) this.findViewById(R.id.btnDrawEnvlope);
		btnDrawEnvlope.setOnClickListener(toolsOnClickListener);

		Button btnDrawEditor = (Button) this.findViewById(R.id.btnDrawSave);
		btnDrawEditor.setOnClickListener(toolsOnClickListener);

		Button btnDrawUndo = (Button) this.findViewById(R.id.btnDrawUndo);
		btnDrawUndo.setOnClickListener(toolsOnClickListener);

		Button btnDrawDeleteNode = (Button) this.findViewById(R.id.btnDrawDeleteNode);
		btnDrawDeleteNode.setOnClickListener(toolsOnClickListener);
	}

	@Override
	public void handleDrawEvent(DrawEvent event) throws FileNotFoundException
	{
		// 将画好的图形（已经实例化了Graphic），添加到mMainGraphicsLayer中并刷新显示
//		mGraphicPolylin = (GraphicPolylin) event.getDrawGraphic();
//		Dot[] dots = mGraphicPolylin.getPoints();
//		int le = dots.length;
		
		mGraphic =  event.getDrawGraphic();
		
		this.mMainGraphicsLayer.addGraphic(mGraphic);
		mMapView.refresh();
	}
}
